<?php
/*
 module:		会员管理
 create_time:	2022-04-07 18:47:50
 author:		
 contact:		
*/

namespace app\backend\controller;

use app\backend\service\VipSysService;
use app\backend\model\VipSys as VipSysModel;
use app\backend\service\ZhMatchmakerLevelService;
use think\facade\Db;

class VipSys extends Admin
{


    /*首页数据列表*/
    function index()
    {
        if (!$this->request->isAjax()) {
            return view('index');
        } else {
            $limit  = $this->request->post('limit', 20, 'intval');
            $offset = $this->request->post('offset', 0, 'intval');
            $page   = floor($offset / $limit) + 1;

            $where             = [];
            $where['vip_name'] = $this->request->param('vip_name', '', 'serach_in');
            $where['status']   = $this->request->param('status', '', 'serach_in');
            $where['wxapp_id'] = session('backend.wxapp_id');
            $order             = $this->request->post('order', '', 'serach_in');    //排序字段 bootstrap-table 传入
            $sort              = $this->request->post('sort', '', 'serach_in');        //排序方式 desc 或 asc

            $field   = 'id,vip_name,price,days,wxapp_id,create_time,status,exchange_num';
            $orderby = ($sort && $order) ? $sort . ' ' . $order : 'id desc';

            $res = VipSysService::indexList(formatWhere($where), $field, $orderby, $limit, $page);
            return json($res);
        }
    }

    /*修改排序开关按钮操作*/
    function updateExt()
    {
        $postField = 'id,status,is_recommend';
        $data      = $this->request->only(explode(',', $postField), 'post', null);
        if (!$data['id']) $this->error('参数错误');
        try {
            VipSysModel::update($data);
        } catch (\Exception $e) {
            abort(config('my.error_log_code'), $e->getMessage());
        }
        return json(['status' => '00', 'msg' => '操作成功']);
    }

    /*添加*/
    function add()
    {
        if (!$this->request->isPost()) {
            //查询红娘等级列表
            $res = ZhMatchmakerLevelService::indexList(formatWhere(['status' => 1]), 100, 1);
            foreach ($res['rows'] as &$row){
                $row['money'] = $commission_money_json[$row['id']] ?? 0;
            }
            $this->view->assign('level_rows', $res['rows']);
            return view('add');
        } else {
            $postField = 'vip_name,price,days,wxapp_id,create_time,status,is_recommend,exchange_num,commission_money_json';
            $data      = $this->request->only(explode(',', $postField), 'post', null);
            $data['commission_money_json'] = json_encode($data['commission_money_json']);
            $res       = VipSysService::add($data);

            return json(['status' => '00', 'msg' => '添加成功']);
        }
    }

    /*修改*/
    function update()
    {
        if (!$this->request->isPost()) {
            $id = $this->request->get('id', '', 'serach_in');
            if (!$id) $this->error('参数错误');
            $info = checkData(VipSysModel::find($id));
            $commission_money_json = [];
            if(! empty($info['commission_money_json'])){
                $tmpJson = json_decode($info['commission_money_json'], true);
                foreach ($tmpJson as $v){
                    $commission_money_json[$v['id']] = $v['money'];
                }
            }
            $this->view->assign('info', $info);

            //查询红娘等级列表
            $res = ZhMatchmakerLevelService::indexList(formatWhere(['status' => 1]), 100, 1);
            foreach ($res['rows'] as &$row){
                $row['money'] = $commission_money_json[$row['id']] ?? 0;
            }
            $this->view->assign('level_rows', $res['rows']);

            return view('update');
        } else {
            $postField = 'id,vip_name,price,days,wxapp_id,create_time,status,is_recommend,exchange_num,commission_money_json';
            $data      = $this->request->only(explode(',', $postField), 'post', null);
            $data['commission_money_json'] = json_encode($data['commission_money_json']);
            $res       = VipSysService::update($data);
            return json(['status' => '00', 'msg' => '修改成功']);
        }
    }

    /*删除*/
    function delete()
    {
        $idx = $this->request->post('id', '', 'serach_in');
        if (!$idx) $this->error('参数错误');
        try {
            VipSysModel::destroy(['id' => explode(',', $idx)], true);
        } catch (\Exception $e) {
            abort(config('my.error_log_code'), $e->getMessage());
        }
        return json(['status' => '00', 'msg' => '操作成功']);
    }

    /*查看详情*/
    function view()
    {
        $id = $this->request->get('id', '', 'serach_in');
        if (!$id) $this->error('参数错误');
        $this->view->assign('info', VipSysModel::find($id));
        return view('view');
    }


}

